<template>
	<view>
		<navbar title="新建个人客户" :autoBack="true"></navbar>
		<scroll-view scroll-y="true" class="scbox">
			<view class="con">
				<view class="con__box">
					<view class="con__box__title">
						<image src="/static/kh/khzl.png" class="con__box__title__icon" mode=""></image>
						客户资料
					</view>
					<view class="con__box__item">
						<view class="con__box__item__label">
							头像
						</view>
						<image :src="URL(form.avatar)" class="con__box__item__tx" mode="aspectFill" @click="upavatar">
						</image>
					</view>
					<view class="con__box__item" @click="navto('/pages/customer/choicetags')">
						<view class="con__box__item__label">
							添加标签
						</view>
						<view class="u-flex u-flex-y-center u-flex-end u-flex-wrap" style="flex: 1;">
							<view class="con__box__item__tag" v-for="(item,index) in tagarr" :key="index">
								{{item.name}}
							</view>
						</view>
						<image src="/static/homex/more2.png" class="con__box__item__m" mode="aspectFill"></image>
					</view>
					<view class="con__box__title1">
						<view class="con__box__title1__line">

						</view>
						客户资料
					</view>
					<view class="con__box__item">
						<view class="con__box__item__label">
							客户全名
						</view>
						<input type="text" v-model="form.username" class="con__box__item__inp" placeholder="请输入客户全名" />
					</view>
					<view class="con__box__item">
						<view class="con__box__item__label">
							手机
						</view>
						<input type="number" v-model="form.mobile" maxlength="11" class="con__box__item__inp"
							placeholder="请输入手机号码" />
					</view>
					<view class="con__box__item">
						<view class="con__box__item__label">
							地点
						</view>
						<view class="con__box__item__inp u-flex u-flex-end u-flex-y-center"
							@click="clickaddr">
							<view class="">
								<view class="con__box__item__inp__timelabel" v-if="!form.address">
									选择地点
								</view>
								<view class="con__box__item__inp__time" v-else>
									{{form.address}}
								</view>
							</view>
						</view>
					</view>
					<view class="con__box__item">
						<view class="con__box__item__label">
							编号
						</view>
						<input type="text" v-model="form.salt" class="con__box__item__inp" placeholder="50个字以内" />
					</view>
					<view class="con__box__item">
						<view class="con__box__item__label">
							借款金额
						</view>
						<input type="number" v-model="form.dedamount" class="con__box__item__inp" placeholder="请填写内容" />
					</view>
					<view class="con__box__item">
						<view class="con__box__item__label">
							月利率
						</view>
						<input type="number" v-model="form.month_rate" class="con__box__item__inp"
							placeholder="请填写内容" />
					</view>
					<view class="con__box__item">
						<view class="con__box__item__label">
							借款日期
						</view>
						<view class="con__box__item__inp u-flex u-flex-end u-flex-y-center"
							@click="show=true,curtimetype=1">
							<view class="">
								<view class="con__box__item__inp__timelabel" v-if="!startdate">
									选择日期
								</view>
								<view class="con__box__item__inp__time" v-else>
									{{startdate}}
								</view>
							</view>
							<view class=""
								style="width: 2rpx;height: 22rpx;background: #888888;opacity: 0.5;margin: 0 14rpx;">

							</view>
							<view class="">
								<view class="con__box__item__inp__timelabel" v-if="!starttime">
									选择时间
								</view>
								<view class="con__box__item__inp__time" v-else>
									{{starttime}}
								</view>
							</view>
						</view>
					</view>
					<view class="con__box__item">
						<view class="con__box__item__label">
							到期日期
						</view>
						<view class="con__box__item__inp u-flex u-flex-end u-flex-y-center"
							@click="show=true,curtimetype=2">
							<view class="">
								<view class="con__box__item__inp__timelabel" v-if="!enddate">
									选择日期
								</view>
								<view class="con__box__item__inp__time" v-else>
									{{enddate}}
								</view>
							</view>
							<view class=""
								style="width: 2rpx;height: 22rpx;background: #888888;opacity: 0.5;margin: 0 14rpx;">

							</view>
							<view class="">
								<view class="con__box__item__inp__timelabel" v-if="!endtime">
									选择时间
								</view>
								<view class="con__box__item__inp__time" v-else>
									{{endtime}}
								</view>
							</view>
						</view>
					</view>
					<view class="con__box__item">
						<view class="con__box__item__label">
							担保方式
						</view>
						<input type="text" v-model="form.guaran" class="con__box__item__inp" placeholder="请填写内容" />
					</view>
					<view class="con__box__item">
						<view class="con__box__item__label">
							还款方式
						</view>
						<input type="text" v-model="form.load_method" class="con__box__item__inp" placeholder="请填写内容" />
					</view>
					<u-button color="#3262FD" @click="submit" text="保存" :loading="lodbtn"
						:customStyle='{width:"628rpx",height:"94rpx",marginTop:"34rpx",marginLeft:0,padding:0,borderRadius:"15rpx",fontSize:"32rpx"}'></u-button>
				</view>
			</view>
		</scroll-view>
		<u-datetime-picker :show="show" @cancel="show=false" @confirm="confirm" v-model="value1"
			mode="datetime"></u-datetime-picker>
	</view>
</template>

<script>
	import {
		wgs84togcj02
	} from "@/utils/toGCj.js"
	import sale from '@/api/_sale.js'
	export default {
		data() {
			return {
				form: {
					"avatar": "",
					"username": "",
					"mobile": "",
					"salt": "",
					"dedamount": "",
					"month_rate": "",
					"loan_date": "",
					"end_date": "",
					"guaran": "",
					"load_method": "",
					"label": "",
					address:""
				},
				tagarr: [],
				startdate: '',
				starttime: '',
				enddate: '',
				endtime: '',
				value1: Number(new Date()),
				show: false,
				curtimetype: 1,
				lodbtn: false
			};
		},
		onShow() {
			this.tagarr = []
			this.tagarr=this.$store.state.tagarr
			if(this.$store.state.curaddr){
				this.form.address=this.$store.state.curaddr.poiaddress
			}
		},
		methods: {
			confirm(e) {
				// console.log(e)
				this.show = false
				if (this.curtimetype == 1) { //借款
					this.startdate = uni.$u.timeFormat(e.value, 'yyyy-mm-dd');
					this.starttime = uni.$u.timeFormat(e.value, 'hh:MM');

				} else {
					this.enddate = uni.$u.timeFormat(e.value, 'yyyy-mm-dd');
					this.endtime = uni.$u.timeFormat(e.value, 'hh:MM');
				}
			},
			clickaddr(){
				
				
				uni.getLocation({
					type: 'wgs84',
					success: (res) => {
						uni.hideLoading()
						let jwd = wgs84togcj02(res.longitude, res.latitude)
						// _this.navto('/pages/tool/map')
						this.navto('/pages/tool/map?latitude='+jwd[1]+'&longitude='+jwd[0])
						// _this.getaddr(,)
					},
					fail: (err) => {
						uni.hideLoading()
						uni.showToast({
							title: '请开启手机定位',
							icon: 'none'
						});
					}
				});
				
			},
			async submit() {
				// console.log(JSON.stringify(this.tagarr))
				// return
				let _this = this
				if (this.tagarr.length > 0) {
					this.form.label = JSON.stringify(this.tagarr).replace(/[\\]/g,'')
				}
				this.form.loan_date = this.startdate + ' ' + this.starttime
				this.form.end_date = this.enddate + ' ' + this.endtime
				// console.log(this.form)
				if (!this.paramsValidate(this.form)) {
					return
				}
				this.lodbtn = true
				let res = await sale.createUser(this.form)
				if (res.data.code == 1) {
					uni.$u.toast('保存成功')
					this.$store.commit('settagarr', [])
					setTimeout(() => {
						_this.lodbtn = false
						uni.navigateBack()
					}, 1000)
				} else {
					_this.lodbtn = false
				}
			},
			paramsValidate(params) {
				let flag = true;
				for (var key in params) {
					if (params[key] != '0' && !params[key]) {
						uni.$u.toast('请将信息填写完整')
						return false; // 终止程序
					}
				}
				return flag;
			},
			upavatar() {
				let _this = this
				uni.chooseImage({
					count: 1,
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.showLoading({
							title: '上传中'
						})
						uni.uploadFile({
							url: 'http://dn.fuliaoxx.com/api/common/upload', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							formData: {
								'user': 'test'
							},
							header: {
								token: uni.getStorageSync('dktoken') || ''
							},
							success: (uploadFileRes) => {
								let res = JSON.parse(uploadFileRes.data)
								uni.hideLoading()
								if (res.code == 1) {
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
									_this.form.avatar = res.data.url
								} else if (res.code == 401) {
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
									setTimeout(() => {
										uni.reLaunch({
											url: '/pages/index/login'
										})
									}, 2000)
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
								}
							},
							fail() {
								uni.$u.toast('上传失败')
								uni.hideLoading()
							}
						});
					}
				});
			}
		},
	}
</script>

<style lang="scss">
	page {
		background-color: #f8f8f8;
	}

	.con {
		padding: 20rpx 30rpx 24rpx;

		&__box {
			background: #FFFFFF;
			border-radius: 30rpx;
			padding-left: 30rpx;
			padding-bottom: 37rpx;

			&__title {
				display: flex;
				align-items: center;
				padding: 33rpx 0 0 0;
				font-family: PingFang SC;
				font-weight: bold;
				font-size: 30rpx;
				color: #1E1E1E;
				line-height: 30rpx;

				&__icon {
					width: 40rpx;
					height: 40rpx;
					margin-right: 14rpx;
				}
			}

			&__item {
				height: 104rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding-right: 32rpx;
				border-bottom: 2rpx solid #F9F9F9;

				&__tag {
					padding: 0 19rpx;
					height: 51rpx;
					background: #F1F5F8;
					border-radius: 5rpx;
					font-family: PingFang SC;
					font-weight: bold;
					font-size: 24rpx;
					color: #626667;
					line-height: 51rpx;
					margin-right: 17rpx;

				}

				&__label {
					font-family: PingFang SC;
					font-weight: 500;
					font-size: 26rpx;
					color: #6D6D6D;
					line-height: 26rpx;
				}

				&__tx {
					width: 76rpx;
					height: 76rpx;
					border-radius: 50%;
					background-color: #6D6D6D;
				}

				&__m {
					width: 12rpx;
					height: 21rpx;
				}

				&__inp {
					flex: 1;
					font-family: PingFang SC;
					font-weight: 500;
					font-size: 26rpx;
					color: #020202;
					line-height: 38rpx;
					text-align: right;

					.uni-input-placeholder {
						font-family: PingFang SC;
						font-weight: 500;
						font-size: 26rpx;
						color: #6D6D6D;
						line-height: 38rpx;
					}

					&__timelabel {
						font-family: PingFang SC;
						font-weight: 500;
						font-size: 26rpx;
						color: #6D6D6D;
						line-height: 38rpx;
					}
				}
			}

			&__title1 {
				font-family: PingFang SC;
				font-weight: 500;
				font-size: 26rpx;
				color: #1E1E1E;
				line-height: 26rpx;
				display: flex;
				align-items: center;
				height: 106rpx;
				border-bottom: 2rpx solid #F9F9F9;

				&__line {
					width: 5rpx;
					height: 24rpx;
					background: #3262FD;
					margin-right: 10rpx;
				}
			}

		}
	}
</style>